<script lang="ts" setup name="Department">
import { CirclePlus, Delete, EditPen } from '@element-plus/icons-vue'
import { cloneDeep } from 'lodash-es'
import { ProTableInstance } from '~/library/components/ProTable/interface'
import { StoreManagement } from '~/src/api/interface/storeManagement'
import { createDepartmentAPI, deleteDepartmentAPI, editDepartmentAPI, getDepartmentAPI } from '~/src/api/storeManagement'
import { departColumn } from '../columns'
import DepartDialog from '../components/DepartDialog.vue'
const dialogRef = ref<InstanceType<typeof DepartDialog>>()
const proTableRef = ref<ProTableInstance>()

const initData = reactive<StoreManagement.DepartmentList>({ id: 0, name: '', status: 1, number: '', remark: '' })
const openDialog = (title: any, row: StoreManagement.DepartmentList) => {
  dialogRef.value?.acceptParams({
    title,
    data: cloneDeep(row),
    api: row.id ? editDepartmentAPI : createDepartmentAPI,
    getTableList: proTableRef.value?.getTableList,
  })
}
const deleteAccount = () => {
  useDeleteData(deleteDepartmentAPI, proTableRef.value as ProTableInstance)
}
</script>

<template>
  <div class="auto-height-container">
    <pro-table ref="proTableRef" :columns="departColumn" :request-api="getDepartmentAPI" title="部门列表">
      <!-- :init-param="initParam" -->
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" :icon="CirclePlus" type="primary" @click="openDialog('新增', initData)">新增部门</el-button>
        <el-button v-auth="'delete'" :icon="Delete" @click="deleteAccount">删除</el-button>
      </template>
      <!-- Expand -->
      <template #expand="scope">
        {{ scope.row }}
      </template>
      <!-- 表格操作 -->
      <template #number="{ row }">
        <!-- <el-button v-auth="'view'" :icon="View" link type="primary" @click="openDialog('查看', scope.row)">查看</el-button> -->
        <el-button v-auth="'edit'" :icon="EditPen" link type="primary" @click.stop="openDialog('编辑', row)">{{ row.number }}</el-button>
      </template>
    </pro-table>
    <depart-dialog ref="dialogRef" />
  </div>
</template>

<style lang="scss" scoped></style>
